const mysql = require("mysql2");
const configuration = require("../utils/configuration");
const pool = mysql.createPool(configuration.sqlConfig);

exports.q = function (sql, params = []) {
  if (!params) {
    console.log("当前查询参数没有值，请查看db.js:7", params);
  }
  return new Promise((resolve, reject) => {
    pool.getConnection(function (err, connection) {
      if (err) throw err;
      connection.query(sql, params, function (err, result) {
        //调试log
        console.log(
          `sql参数为:${
            params || "空"
          }，sql语句为:${sql}，结果为:${JSON.stringify(result)}`
        );
        connection.release();
        if (err) {
          console.log("db出现异常");
          reject(err);
        } else {
          resolve(result);
        }
      });
    });
  });
};
